package com.tencent.service.impl;

import com.tencent.entity.OrderSetting;
import com.tencent.mapper.OrderSettingMapper;
import com.tencent.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author: Fame
 * @date: 2022-08-16 10:02
 * @description:
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public int insert(OrderSetting orderSetting) {
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(orderSetting.getOrderDate());
        OrderSetting os = orderSettingMapper.findOneBtOrderDate(dateStr);
        if (null != os){
            return orderSettingMapper.updateNumber(orderSetting.getNumber(),dateStr);
        }else {
            return orderSettingMapper.insert(orderSetting);
        }
    }

    @Override
    public List<OrderSetting> findByOrderDate(String startDate, String endDate) {
        return orderSettingMapper.findListByOrderDate(startDate,endDate);
    }

    @Override
    public int updateReservations(Date orderDate) {

        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
        OrderSetting os = orderSettingMapper.findOneBtOrderDate(dateStr);
        Integer reservations = os.getReservations();
        Integer number = os.getNumber();
        if (reservations <= number){
            reservations += 1;
            int count = orderSettingMapper.updateReservations(reservations, dateStr);
            return count;
        }
        return 0;
    }
    @Override
    public int updateDelReservations(Date orderDate) {


        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
        OrderSetting os = orderSettingMapper.findOneBtOrderDate(dateStr);
        Integer reservations = os.getReservations();
        Integer number = os.getNumber();
        if (reservations <= number){
            reservations -= 1;
            int count = orderSettingMapper.updateReservations(reservations, dateStr);
            return count;
        }
        return 0;
    }
}
